<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/23
 * Time: 17:08
 */

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/../Workerman/Autoloader.php';

$worker = new Worker();

/**
 * aes加密
 * @param $str
 * @return string
 */
function aesEncrypt($str,$ak,$sk)
{
    return base64_encode(openssl_encrypt($str,'AES-128-CBC',$ak, 1, $sk));
}

/**
 * aes解密
 * @param $str
 * @return string
 */
function aesDecrypt($str,$ak,$sk)
{
    return openssl_decrypt(base64_decode($str), 'AES-128-CBC',$ak, 1, $sk);
}

function createNonce($len){
    $strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
    return substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),$len);
}
$ak = 'asdawsfesadfgews';
$sk = '3333111111111112';
/*$ak = 'VkhRW0C521ZMYuqF';
$sk = 'CE0J99-gsI-acW1P';*/

$worker->onWorkerStart = function($worker)use($ak){
    $local = '127.0.0.1:9501';
    $online = '129.211.114.112:55049';

    $con = new AsyncTcpConnection('ws://'.$local);
    $con->headers = array('access_key' =>$ak);
    $con->onConnect = function($con) {
      # $appUserId = 'T891194838';
       #$appUserId = 'T7559503';
       $appUserId = 'T157249531';
    #    $appUserId = 'pSbnr5f64609876698';
       # $appUserId = 'pSbnr5f64609876698';
      # $appUserId = 'I6q1p5f6460e4346bc';
       #$appUserId = 'T891194838';
       #$appUserId = '5vG8O5f6421234e55b';
     #$appUserId = 'pSbnr5f64609876698';
     #  $appUserId = 'jianjiantest';
     #   $appUserId = '1Wdr25f64903e549b1';
     # $appUserId = 'qkSwA5f3deb5482085';
      # $appUserId = '80utM5f3de521803b9';
     # $appUserId = 'yR7Y25f3f92866777f';
        $ak = 'asdawsfesadfgews';
        $sk = '3333111111111112';

     /*   $ak = 'VkhRW0C521ZMYuqF';
        $sk = 'CE0J99-gsI-acW1P';*/
        #ping
      /*  $form = array(
            'ping'
        );
        $action = 'ping';
        #发送ping
        $message = array(
            'nonce'=>time(),
            'data'=>aesEncrypt(json_encode($form)),
            'action'=>$action,
            'class'=>'Server',
            'access_key'=>'VkhRW0C521ZMYuqF',
        );*/

           #变更账户
       /* $form = array(
            'user_id'=>17,
            'nickname'=>'张三',
            'head'=>'http://pic2.sc.chinaz.com/Files/pic/pic9/202006/bpic20542_s.jpg'
        );
        $action = 'updateUserInfo';

        $message = array(
            'nonce'=>time(),
            'data'=>aesEncrypt(json_encode($form)),
            'action'=>$action,
            'class'=>'Server',
            'access_key'=>'VkhRW0C521ZMYuqF',
        );*/
       #登录id29
     $extra = array(
            'access_key'=>$ak,
            'app_user_id'=>$appUserId,
           'nickname'=>'imim',
          'head'=>'http://img.xmfish.com/attachment/upload/middle/67/2233367.jpg',
        # 'token'=>'2651600667695'
       );

        $action = 'login';

        $message = array(
            'nonce'=>time(),
            'content'=>' ',
            'action'=>$action,
            'extra'=>$extra
        );

        $message = array_merge($message,    $extra);

      $con->send(aesEncrypt(json_encode($message),$ak,$sk));

        $time_interval = 3;
        $i = 1;
        $recent = 0;
        \Workerman\Timer::add($time_interval, function() use($ak,$sk,$con,$i,$appUserId,$recent)
        {

          #最近联系人
            $extra = array(
            );
            $action = 'recent';
            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );
            #七牛授权
           /*$extra = array(

            );
               $action = 'qiniuAuth';

            $message = array(
                'nonce'=>time(),
                'content'=>'DxOHay1593939803100.556885',
               'action'=>$action,
               'extra'=>$extra,
            );*/
            #单聊
            /*$extra = array(
            );
            $action = 'chat';

            $message = array(
                'nonce'=>time(),
                'content'=>'hello2',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'1Wdr25f64903e549b1',
                'type'=>1,
            );*/
        #同步系统消息
          /*  $extra = array(
                'msg_id'=>1315
            );
            $action = 'syncSysMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
       #一对一系统消息
           /* $extra = array(
                'asd'=>'fuck'
            );
            $action = 'systemMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'9a4297c33d9328934e496a4074863cfs',
                'type'=>1,
            );*/

        #群聊
          #{"type":1,"group_id":"19","content":"a","action":"groupChat","nonce":"1594191881721FQK5l9"}
        $i =2445;
        while ($i<2455){
            $extra = array(

            );
            $action = 'groupChat';
            $message = array(
                'nonce'=>time().mt_rand(10000,99999),
                'content'=>'群聊自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>$i,
                'type'=>1,
            );
            sleep(mt_rand(1,3));
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));
            echo $i.'群聊发送成功'.PHP_EOL;
            $i = $i+1;
        }

         #创建群
/*        $i = 0;
        while ($i<100){
            $extra = array(
                'app_user_ids'=>'T7559503,T157249531'
            );
            $action = 'createGroup';
            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));
            sleep(1);
            $i = $i+1;
        }*/
       #邀请入群
    /* $extra = array(
                'app_user_ids'=>'gRK0j5f58a53c90201,d22beec2cc5e355bd4898a7b7ce3031a',
               'group_id'=>'476'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #我的群
    /*    $extra = array(
                'type'=>0,
                'page'=>1,
                 'limit'=>10,
                #'updated_at'=>1599817642000
            );
            $action = 'myGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #群成员详情
        /* $extra = array(
                'group_id'=>'3451',
                'page'=>1,
               'limit'=>2,
              #   'updated_at'=>1597306690000
            );
            $action = 'groupMemberList';

            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'25',
                'type'=>1,
            );*/
        #编辑群
        /*   $extra = array(
                'group_id'=>'1878',
                'group_name'=>1,
                'invite_mode'=>"1"
            );
            $action = 'editGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'25',
                'type'=>1,
            );*/
            #获取客服
        /*  $action = 'getServiceGroup';
            $extra = array(

            );
            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'25',
                'type'=>1,
            );*/

            #移除群成员
        /* $extra = array(
                'group_id'=>'358',
                'app_user_ids'=>'jianjianxiansheng,572f01b0988e33369ad8ff0d3cd440a9'
            );
            $action = 'removeMember';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
           #退出群
         /*$extra = array(
                'group_id'=>'332',
            );
            $action = 'leaveGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/

         #邀请入群
          /*  $extra = array(
                'group_id'=>'3',
                'app_user_ids'=>'jianjianxianshen1g'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
/*  $extra = array(
                'group_id'=>'3',
                'app_user_ids'=>'jianjianxianshen1g'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #获取群历史消息
 /* $extra = array(
                'group_id'=>'484',
               // 'msg_id'=>1,
              'count'=>20
            );
            $action = 'groupChatHistory';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/

       #获取单聊历史消息
     /*$extra = array(
                'app_user_id'=>'pSbnr5f64609876698',
                #'msg_id'=>'5957',
                'count'=>20
            );
            $action = 'chatHistory';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #获取群详情
  /*       $extra = array(
                'group_id'=>'1'
            );
            $action = 'getGroupDetail';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
  #主动发送系统消息
        /*    $extra = array(
                'msg_id'=>$id
            );
            $action = 'pushMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         /*   $extra = array(
            );
            $action = 'groupChat';
            $message = array(
                'nonce'=>time().mt_rand(10000,99999),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>$id,
                'type'=>1,
            );
            sleep(mt_rand(0,2));
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));
            $id = $id+1;
            echo '发送'.PHP_EOL;*/

        #获取客服群getServiceGroup
     /*    $extra = array(
            );
            $action = 'getServiceGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #客服发消息
     /*  $extra = array(

            );
            $action = 'groupChat';

            $message = array(
                'nonce'=>time().'asfse',
                'content'=>'您好，客服售前咨询-高蚁材为您服务。😥🤫😪😪🥳💘😪🤔🤔🤔🤔'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'106',
                'type'=>1,
            );*/
     #修改群信息
         /*$extra = array(
                'group_id'=>'561',
                'group_name'=>'群的阿萨德1111',
                 'banned_to_post'=>'1',
                'invite_mode'=>'0',
            );
            $action = 'editGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #群管理员设置
         /*   $extra = array(
                'group_id'=>'177',
                'role'=>2,
                'app_user_ids'=>'qkSwA5f3deb5482085,79HF55f3deccdd4226'
            );
            $action = 'groupMemberSetting';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #加入黑名单
  /*          $extra = array(
                'app_user_id'=>'jianjianxianshen1g'
        );
            $action = 'getBlacklist';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #群消息设置
        /*  $extra = array(
                'group_id'=>'177',
                'msg_setting'=>1,
               'top'=>1
            );
            $action = 'groupSetting';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
          #主动同步信息
       /*$condition = array(
               [ 'type'=>1,
                   'id'=>'wW1QL5f641b4b69a6f'
               ],
           [ 'type'=>1,
               'id'=>'1Wdr25f64903e549b1'
           ]
            );
            $extra = array(
                'condition'=>$condition,
            );
            $action = 'syncLastMsg';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
       #更新用户信息
           /* $extra = array(
                'nickname'=>'我的最帅的开发小哥',
                'head'=>'https://fanyiapp.cdn.bcebos.com/cms/image/58eb5ccdedfd017daa2e7d4e48d43891.jpg'
            );
            $action = 'updateUserInfo';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
           #获取其他人用户信息
        /* $extra = array(
                'app_user_id'=>'jianjianxiansheng',
            );
            $action = 'getOtherUserInfo';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
            #清理消息
         /* $extra = array(
                'msg_type'=>1,
                'id'=>'I6q1p5f6460e4346bc',
            );
            $action = 'clearMsg';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #自定义系统消息
        /* $extra = array(
                'sub_type'=>'personal',
                'associated_id'=>'pSbnr5f64609876698',
                'data'=>'asdfasfsef'
            );
            $action = 'diySysMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/

         $con->send(aesEncrypt(json_encode($message),$ak,$sk));
        echo '发送时间：'.date("Y-m-d H:i:s").PHP_EOL;

        });
    };

    $con->onMessage = function($con, $data) {
        $ak = 'asdawsfesadfgews';
        $sk = '3333111111111112';
        /*$ak = 'VkhRW0C521ZMYuqF';
        $sk = 'CE0J99-gsI-acW1P';*/
        echo $data,PHP_EOL;

        $dataArr = json_decode(aesDecrypt($data,$ak,$sk),true);

     var_dump(  $dataArr );
        echo date('Y-m-d H:i:s',$dataArr['time']/1000).PHP_EOL;
        echo '接收时间：'.date("Y-m-d H:i:s").PHP_EOL;


        file_put_contents('json',json_encode($dataArr).PHP_EOL,FILE_APPEND);
        echo PHP_EOL;
    };

    $con->connect();
};

Worker::runAll();